﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.BaseData
{
    public partial class EditStudentInfo :Base.BasePage
    {
        protected int IDTemp;
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("XSSJ");
            strPageUserType.Add("mana");
            strPageUserType.Add("student");
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "你无权查看本页！");
                }
                else
                {
                    int ID = int.Parse(Request.QueryString["ID"].ToString());
                    IDTemp = ID;
                    InitDDl();
                    InitBind(ID);
                }
                tbC.Style.Add("display", "none");
                tbM.Style.Add("display", "none");

                textboxCode.Attributes.Add("readOnly", "true");
            }
        }

        protected int StudentID
        {
            get
            {
                return IDTemp;
            }
        }

        protected void InitDDl()
        {
            //性别
            ExtendBLL.Dictionary BD = new ExtendBLL.Dictionary();
            ddlSex.DataSource = BD.GetItemListByDClass("201");
            ddlSex.DataValueField = "Code";
            ddlSex.DataTextField = "Name";
            ddlSex.DataBind();
            //年级
            ExtendBLL.SysSetting ES = new ExtendBLL.SysSetting();
            int Year = ES.GetCurrentSetting().CurrentYear;
            int S = int.Parse(ES.GetCurrentSetting().CurrentSemester.Value.ToString());
            ddlLJ.DataSource = Common.GradeHelper.GetCurrentGrade(Year, S, false);
            ddlLJ.DataBind();
        }

        protected void InitBind(int ID)
        {
            LabMS.BLL.Student BStudent = new LabMS.BLL.Student();
            List<LabMS.Model.Student> list = new List<LabMS.Model.Student>();
            list = BStudent.GetModelList("ID=" + ID);
            textboxCode.Text = list[0].Student_Code;
            textboxName.Text = list[0].Student_Name;
            ddlSex.SelectedValue = list[0].Student_Sex;//性别
            ddlLJ.SelectedValue = list[0].Student_Grade;//年级

            ExtendBLL.Class EClass = new ExtendBLL.Class();
            if (list[0].Student_Pro != null && !string.IsNullOrEmpty(list[0].Student_Pro.Value.ToString()))
            {
                textboxMajor.Text = EClass.ProessionalNameByID(int.Parse(list[0].Student_Pro.Value.ToString()));//专业
                tbM.Text=list[0].Student_Pro.Value.ToString();
            }
            if (list[0].ClassID != null && !string.IsNullOrEmpty(list[0].ClassID.Value.ToString()))
            {
                textboxClass.Text = EClass.GetClassNameByClassID(int.Parse(list[0].ClassID.Value.ToString()));//班级
                tbC.Text = list[0].ClassID.Value.ToString();
            }
            if (list[0].Student_EntryDate != null)
            {
                tbRXLY.Text = list[0].Student_EntryDate.Value.ToShortDateString();
            }
            tbSum.Text = list[0].Student_Summary;
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(textboxCode.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "学号不能为空！");
                textboxCode.Focus();
            }
            else if (string.IsNullOrEmpty(textboxName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "姓名不能为空！");
                textboxName.Focus();
            }
            #endregion
            else
            {
                LabMS.Model.Student MStudent = new LabMS.Model.Student();
                MStudent.ID = int.Parse(Request.QueryString["ID"].ToString());
                MStudent.Student_Code = textboxCode.Text.Trim();
                MStudent.Student_Name = textboxName.Text.Trim();
                MStudent.Student_Sex = ddlSex.SelectedValue;
                MStudent.Student_Summary = tbSum.Text.Trim();

                if (!string.IsNullOrEmpty(tbC.Text.Trim()))
                {
                    MStudent.ClassID = int.Parse(tbC.Text.Trim()); //存ID
                }
   
                if (!string.IsNullOrEmpty(tbM.Text.Trim()))
                {
                    MStudent.Student_Pro = int.Parse(tbM.Text.Trim());//存ID
                }

                if (!string.IsNullOrEmpty(tbRXLY.Text.Trim()))
                {
                    string[] DateTimeList = { "yyyy/MM/dd", "yyyy/MM", "yyyy/M", "yyyy-MM-dd", "yyyy-M-d", "yyyyMM", "yyyyMMdd", "yyyy-MM" };
                    MStudent.Student_EntryDate = DateTime.ParseExact(tbRXLY.Text.Trim(), DateTimeList, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowInnerWhite);
                }
                MStudent.Student_Grade = ddlLJ.SelectedValue;
                LabMS.BLL.Student BStudent = new LabMS.BLL.Student();
                try
                {
                    BStudent.Update(MStudent);
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('保存成功');window.opener.location.replace(window.opener.location);self.close();</script>");
                }
                catch
                {
                    Common.JShelper.JSAlert(this.Page, "", "保存失败");
                }
            }
        }
    }
}

